package game;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.LinkedList;

public class ButtonManager {

	private static ButtonManager instance;
	MatrixCell[][] m1, m2, mR; // firstMatrix, secondMatrix, resultMatrix
	LinkedList<Button> numbers = new LinkedList<Button>();
	LinkedList<Button> signs = new LinkedList<Button>();
	Button comma, clear, randomButton;
	
	private ButtonManager()
	{
		CriarBotoes();
	}
	
	public static ButtonManager getInstace()
	{
		if(instance == null)
			instance = new ButtonManager();
			
		return instance;
		
	}

	
	public void draw(Graphics2D g2d)
	{
		for (Button number: numbers)
			number.draw(g2d);
		for (Button sign: signs)
			sign.draw(g2d);
		comma.draw(g2d);
		clear.draw(g2d);
		randomButton.draw(g2d);
	}
	
	public MatrixCell[][] UpdateButtonMatrix(MatrixCell[][] m, Point curS)
	{
		for (int i = 0; i < numbers.size(); i++)
		{	
			if (i < signs.size())
			{
				signs.get(i).update();
				if (signs.get(i).clicked == true)
					m[curS.x][curS.y] = ButtonLogic.getInstace().SignLogic(m[curS.x][curS.y], i);
			}
			numbers.get(i).update();
			if (numbers.get(i).released)
			{
				int n = numbers.indexOf(numbers.get(i));
				m[curS.x][curS.y] = ButtonLogic.getInstace().NumberLogic(m[curS.x][curS.y], n);
			}
		
			
		}
				
		comma.update();
		if (comma.released)
			m[curS.x][curS.y] = ButtonLogic.getInstace().CommaLogic(m[curS.x][curS.y]);
		
		clear.update();
		if (clear.released)
		{
			m[curS.x][curS.y].value = null;
			m[curS.x][curS.y].i = 0;
			m[curS.x][curS.y].d = 0;
		}
		
		this.randomButton.update();
		if(randomButton.released)
		{
			MatrixManager.getInstace().RandomizarMatriz();
		}
		return m;
	}
	
	public void CriarBotoes()
	{
		for (int i = 0; i < 10; i++)
		{
			if (i < 2)
				signs.add(new Button("/images/Signs.png", new Point2D.Double(5 + i * 740, 475), new Point2D.Double(50, 50), 
															  new Point2D.Double(0, i * 50), new Point2D.Double(50, 50)));
			numbers.add(new Button("/images/Numbers.png", new Point2D.Double(105 + i * 60, 525), new Point2D.Double(50, 50), 
															  new Point2D.Double(0, i * 50), new Point2D.Double(50, 50)));
		}
		comma = new Button("/images/Comma.png", new Point2D.Double(30, 530), new Point2D.Double(60, 40), 
				  								new Point2D.Double(0, 0), new Point2D.Double(50, 50));
		clear = new Button("/images/Return.png",	new Point2D.Double(710, 530), new Point2D.Double(60, 40), 
													new Point2D.Double(0, 0), new Point2D.Double(50, 50));
		
		this.randomButton = new Button("/images/RandomButton.png",	new Point2D.Double(250, 470), new Point2D.Double(300, 20), 
																new Point2D.Double(0, 0), new Point2D.Double(300, 20));
	}
}
	
	
